<?php

class BlogModel extends Model {
	public $table = 'blog';
	public function getAll(){
		$result = $this->count();
		$page = new Page($result,5);
		$pageStr = $page->showli();
		$limit = $page->limit();
		$sql ="select * from blog b  join category c on b.cid=c.cid order by bid desc  LIMIT {$limit} ";
		$blog = $this->query($sql);
		return array('blog'=>$blog,'page'=>$pageStr);
	}


	public function addBlog(){
		//有上传缩略图文件时,处理上传文件
		if(!empty($_FILES['thumb']['name'])){
			$upload =new Upload;
			$files = $upload->upload();
			$_POST['thumb']=$files[0]['path'];
		}
		//发表时间
		$_POST['addtime']=time();
		return $this->insert($_POST);
	}
	//修改博客
	public function editBlog(){
		//有上传缩略图文件时,处理上传文件
		if(!empty($_FILES['thumb']['name'])){
			//如果有原图时,删除他..
			if(is_file($_POST['thumb'])){
				unlink($_POST['thumb']);
			}
			$upload =new Upload;
			$files = $upload->upload();
			$_POST['thumb']=$files[0]['path'];
		}
		//发表时间
		$_POST['addtime']=time();
		return $this->where("bid={$_POST['bid']}")->update($_POST);
	}

	public function deleteBlog($data){
		unlink($data['thumb']);
		return $this->where("bid = {$data['bid']}")->delete();
	}
}


?>